package com.lk.api.own;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.lk.api.R;

/**
 *
 * @author lk
 *
 */
public class MusicService extends Service {
	// ���ֲ�����
	MediaPlayer player;
	// �ӿڶ���
	IBinder MyBinder = new MyBinder();

	// ����IBinder�ӿ�ʵ��
	class MyBinder extends Binder {
		public String helloWorld(String name) {

			return "you name is:" + name;
		}

		/**
		 * @return ���ض��� StartService
		 */
		public MusicService getService() {
			return MusicService.this;
		}

	}

	// public MusicService(String name) {
	// // super(name);
	// // TODO Auto-generated constructor stub
	// }
	//
	// public MusicService() {
	// // super("ssss");
	// // TODO Auto-generated constructor stub
	// }

	// public void helloService() {
	// Log.d("helloService", "ok");
	// for (int i = 0; i < 10; i++) {
	// try {
	// Thread.sleep(1000);
	// Log.d("run", " " + i);
	// } catch (InterruptedException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// }

	// @Override
	// protected void onHandleIntent(Intent intent) {
	// // TODO Auto-generated method stub
	// Log.d("onHandleIntent", "onHandleIntent");
	// helloService();
	// for (int i = 0; i < 10; i++) {
	// try {
	// Thread.sleep(1000);
	// Log.d("run", " " + i);
	// } catch (InterruptedException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// }

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.d("onCreate", "onCreate");
		player = MediaPlayer.create(MusicService.this, R.raw.music);
		super.onCreate();
	}

	// ������� �����onStartCommandȡ��
	// @Override
	// public void onStart(Intent intent, int startId) {
	// // TODO Auto-generated method stub
	// Log.d("onStart", "onStart");
	// super.onStart(intent, startId);
	// }

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.d("onStartCommand", "onStartCommand");
		int mPlayback;
		if (intent != null) {
			mPlayback = intent.getExtras().getInt("Playback");
			switch (mPlayback) {
			case 1:
				player.start();

				Log.d("mPlayback", "" + mPlayback);
				break;
			case 2:
				player.pause();
				Log.d("mPlayback", "" + mPlayback);
				break;
			case 3:
				player.stop();
				Log.d("mPlayback", "" + mPlayback);
				player.release();
				player = null;
				player = MediaPlayer.create(MusicService.this, R.raw.music);
				break;

			default:
				break;
			}

		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.d("onDestroy", "onDestroy");
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("onBind", "onBind");
		// ��¶IBinder�ӿڶ���
		return MyBinder;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("onUnbind", "onUnbind");
		return super.onUnbind(intent);
	}

}
